﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.UI;

namespace Nasa8x.Mvc.Helpers
{

    public class GridColumn
    {
        public string HeaderText { get; set; }

        public string DataField { get; set; }

    }

  public static class DataGridHelper
    {

        //http://mvcsharp.wordpress.com/2010/02/11/building-a-data-grid-in-asp-net-mvc/

        public static string DataGrid<T>(this HtmlHelper helper)
        {
            return DataGrid<T>(helper, null, null);
        }

        public static string DataGrid<T>(this HtmlHelper helper, object data)
        {
            return DataGrid<T>(helper, data, null);
        }

        public static string DataGrid<T>(this HtmlHelper helper, object data, List<GridColumn> columns)
        {
            //Get items
          
            string[] headers;
            string[] dataFields;

            //Get column names
            if (columns == null)
            {
                dataFields= headers = typeof(T).GetProperties().Select(p => p.Name).ToArray();
                
            }
                
            else
            {
                headers = columns.Select(p => p.HeaderText).ToArray();
                dataFields = columns.Select(p => p.DataField).ToArray();
            }

            return DataGrid<T>(helper, data, headers, dataFields);
        }

        public static string DataGrid<T>(this HtmlHelper helper, object data, string[] headers, string[] columns)
        {
            //Get items
            var items = (IEnumerable<T>)data;
            if (items == null)
                items = (IEnumerable<T>)helper.ViewData.Model;

            //Get column names
            if (columns == null)
                columns = typeof(T).GetProperties().Select(p => p.Name).ToArray();

          
            //Create HtmlTextWriter
            var writer = new HtmlTextWriter(new StringWriter());

            //Open table tag
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            //Render Table Header
            writer.RenderBeginTag(HtmlTextWriterTag.Thead);
            RenderHeader(helper, writer, headers);
            writer.RenderEndTag();

            // Render table body
            writer.RenderBeginTag(HtmlTextWriterTag.Tbody);
            foreach (var item in items)
                RenderRow<T>(helper, writer, columns, item);
            writer.RenderEndTag();

            //Close  table tag
            writer.RenderEndTag();

            //return the string
            return writer.InnerWriter.ToString();
        }


        private static void RenderHeader(HtmlHelper helper, HtmlTextWriter writer, string[] columns)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            foreach (var columnName in columns)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Th);
                var currentAction = (string)helper.ViewContext.RouteData.Values["action"];
                var link = helper.ActionLink(columnName, currentAction, new { sort = columnName });
                writer.Write(link);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }

        public static void RenderRow<T>(HtmlHelper helper, HtmlTextWriter write, string[] columns, T item)
        {
            write.RenderBeginTag(HtmlTextWriterTag.Tr);
            foreach (var columnName in columns)
            {
                write.RenderBeginTag(HtmlTextWriterTag.Td);
                var value = typeof(T).GetProperty(columnName).GetValue(item, null) ?? String.Empty;
                write.Write(helper.Encode(value.ToString()));
                write.RenderEndTag();
            }
            write.RenderEndTag();
        }

    }
}

